@extends('layouts/app')

@section('content')
{{-- manager设备管理--}}

<div class="weui-pay ">

    <div class="weui-cells">
        <div class="weui-cell">
            <div class="weui-cell__bd">
                <p>部门</p>
            </div>
            <div class="weui-cell__bd">
                <input class="weui-input" name="department" placeholder="前台">
            </div>
        </div>
    </div>

    <div class=" weui-cells weui-cells_checkbox">
        <div class="facility">
            <label class="weui-cell" for="s11">
                <div class="weui-cell">
                    <p>全部打印</p>
                </div>
                <div class="weui-cell__hd ">
                    <p>是</p>
                </div>
                <div class="weui-cell__hd">
                    <input type="radio" class="weui-check" value='0' name="is_useall" id="s11">
                    <i class="weui-icon-checked"></i>
                </div>
            </label>

            <label class="weui-cell" for="s12">
                <div class="weui-cell__hd">
                    <p>否</p>
                </div>
                <div class="weui-cell__hd">
                    <input type="radio" name="is_useall" value='1' class="weui-check" id="s12" >
                    <i class="weui-icon-checked"></i>
                </div>
            </label>
        </div>
        <div class="facility">
            <label class="weui-cell ">
                <div class="weui-cell ">
                    <p>打印份数:</p>
                </div>


                <div class="weui-cell__bd">
                    <input class="weui-input" id="priter_number" name="priter_number">
                </div>

            </label>

        </div>

        {{--菜品分类--}}
        <div class="foodType">
            <p>所有菜系</p>
            <div class="weui-flex foodTypeList">
                {{--<label for="x1" class="weui-agree">--}}
                    {{--<input id="x1" type="checkbox" class="weui-agree__checkbox">--}}
                    {{--<span class="weui-agree__text">菜品分类1</span>--}}
                {{--</label>--}}

                {{--<label for="x2" class="weui-agree">--}}
                    {{--<input id="x2" type="checkbox" class="weui-agree__checkbox">--}}
                    {{--<span class="weui-agree__text">菜品分类1</span>--}}
                {{--</label>--}}
                {{--<label for="x3" class="weui-agree">--}}
                    {{--<input id="x3" type="checkbox" class="weui-agree__checkbox">--}}
                    {{--<span class="weui-agree__text">菜品分类1</span>--}}
                {{--</label>--}}
            </div>
        </div>
    </div>
    <div class="demos-content-padded">
        <a href="javascript:;" class="weui-btn weui-btn_blue open-popup">保存</a>
    </div>


</div>
<script>
  //声明var变量 sid方法getRequest 取值 .sid  .rid
  var sid = getRequest().sid,
          rid = getRequest().rid;
     //function获取函数Equipment
    function getEquipment () {
        //.post方法请求数据接口
        $.post("/api/printer/one", {shop_id: sid, id: rid}, function (res) {
            if(typeof res === 'string') {
            //判断是否字符串,如果是就转换没对象
                res = JSON.parse(res);
            }
            setData(res.data);
        });
    };

  getEquipment();


  // 获取商家菜单数据
  function getShopsMenus (dData) {
      $.get("/api/getshopMenu", {id: sid}, function (res) {
          if (typeof res === 'string') {
              res = JSON.parse(res);
          }
            var data = {};
          data['type'] = 'selectFoodTypeList';
          data['element'] = '.foodTypeList';
          data['data'] = res.data.menu;
          data['dData'] = dData || '';

          A.init(data);
      });
  }


  // 页面赋值
  function setData(data) {

      $("input[name='department']").val(data.department);
      $("input[name='priter_number']").val(data.priter_number);

      // 如果打印所有，0隐藏菜系，否则 1显示
      if (!data.is_useall) {
          $("#s11").attr("checked", "checked");
          hideFoodPanel();
      } else {
          // 显示
          $("#s11").removeAttr("checked", "checked");
          $("#s12").attr("checked", "checked");
          getShopsMenus(data.cuisine);
          showFoodPanel();
      }
  }

  // 选择是与否
  $("input[name='is_useall']").change(function () {
          var value = $(this).val();
          console.log("value", value);
          // 否
          if (value === '0') {

              hideFoodPanel();
          } else {
              // 是c
              // 如果面板上没有值，发起一次请求
              console.log("666");
              var hasC = $(".foodTypeList").children().length;
              if (!hasC) {
                  getShopsMenus();
              }
              // 显示列表
              $(".foodType").show();

          }
      });


  // 显示所有菜系面板
    function showFoodPanel () {
        $(".foodType").show();
    }
  // 隐藏所有菜系面板
  function hideFoodPanel () {
      $(".foodType").hide();
  }

  // 保存数据
  $(".open-popup").click(function () {
      var data = {};
      data['shop_id'] = sid; // 店铺ID
      data['id'] = rid; // 设备ID
      data['department'] = $("input[name='department']").val(); // 部门
      data['priter_number'] = $("input[name='priter_number']").val(); // 打印份数
      data['cuisine'] = getCuisine(); // 打印份数
      data['is_useall'] = $("input[name='is_useall']:checked").val(); // 是否全部打印

      console.log("data", data);

      $.post("/api/printer/edit", data, function (res) {
          if (typeof res === 'string') {
              res = JSON.parse(res);
          }

          $.alert(res.info, function () {
              if (res.code == 200) {
                  // location.reload();
                  history.go(-1);
              }
          });
          // console.log("res", res);
      })

  });

    // 获取用户选择的菜品分类
  function getCuisine () {
      var str = "";

      $(".foodTypeList input").each(function (index, value) {
          // 判断是否选中
          var checked = $(this).prop('checked'),
              val = $(this).val();

          if (checked) {
              str += val + ',';
          }
      });


      return str;
  }
</script>
@endsection
@extends('layouts/footer')
